﻿Imports System.Data.OleDb


Public Class frmTiepNhanPhong
    Dim arrLP(30) As String
    Dim iMaLoaiPhong As Integer

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        If (MessageBox.Show("Bạn có chắc chắn muốn thoát không?", "Thoát", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes) Then
            Me.Close()
            Global.System.Windows.Forms.Application.Exit()
        End If
    End Sub


    Private Sub frmTiepNhanPhong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            LoadPhong()

            'Load danh sach LOAIPHONG
            Dim str = String.Format("select * from LOAIPHONG")
            Dim dLayLP As New SQLProvider()

            Dim iKQ = dLayLP.ExecuteReader(str)
            Dim i As Integer = 0

            While iKQ.Read()
                arrLP(i) = String.Format("{0},{1},{2}", iKQ.GetInt32(0), iKQ.GetString(1), iKQ.GetDecimal(2))
                cbbLoaiPhong.Items.Add(iKQ.GetString(1))
                i += 1
            End While
            cbbLoaiPhong.SelectedIndex = 0
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub LoadPhong()
        Dim kq As New DataTable
        Dim Loai_ket_noi As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
        Dim CSDL As String = "Data Source=QuanLyPhongTro.mdb"
        Dim Chuoi_ket_noi As String = Loai_ket_noi & CSDL
        Dim Ket_noi As New OleDbConnection(Chuoi_ket_noi)
        Dim Bo_thich_ung As New OleDbDataAdapter("select * from PHONG", Ket_noi)
        Bo_thich_ung.FillSchema(kq, SchemaType.Source)
        Bo_thich_ung.Fill(kq)
        grdPhongMoi.DataSource = kq
        Ket_noi.Close()
    End Sub

    Private Sub cbbLoaiPhong_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbLoaiPhong.SelectedValueChanged
        For i As Integer = 0 To arrLP.Length
            If (arrLP(i) = Nothing) Then
                Return
            ElseIf (arrLP(i).IndexOf(cbbLoaiPhong.SelectedItem) > 0) Then
                txtDonGia.Text = arrLP(i).Split(",")(2).Trim().ToString()
                iMaLoaiPhong = arrLP(i).Split(",")(0).Trim()
                Exit For
            End If
        Next
    End Sub

    Private Sub btThemPhong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThemPhong.Click
        Try
            Dim phong As New PhongDTO()
            phong.TenPhong = txtTenPhong.Text
            phong.GhiChu = txtGhiChu.Text
            If (cbTinhTrangP.Checked) Then
                phong.TinhTrangPhong = "Co khach"
            Else
                phong.TinhTrangPhong = "Phong trong"
            End If
            phong.MaLoaiPhong = iMaLoaiPhong

            Dim str = String.Format("insert into PHONG (TenPhong, MaLoaiPhong, TinhTrangPhong, GhiChu) values ('{0}',{1},'{2}','{3}')",
                                    phong.TenPhong, phong.MaLoaiPhong, phong.TinhTrangPhong, phong.GhiChu)
            Dim dInserKH As New SQLProvider()
            Dim iKQ As Integer = -1
            iKQ = dInserKH.ExecNoneQuery(str)

            If (iKQ < 1) Then
                MessageBox.Show("Khong the them moi phong.")
            Else
                LoadPhong()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCapNhat.Click
        Try
            Dim phong As New PhongDTO()
            phong.MaPhong = txtMaPhong.Text
            phong.TenPhong = txtTenPhong.Text
            phong.GhiChu = txtGhiChu.Text
            If (cbTinhTrangP.Checked) Then
                phong.TinhTrangPhong = "Co khach"
            Else
                phong.TinhTrangPhong = "Phong trong"
            End If
            phong.MaLoaiPhong = iMaLoaiPhong

            Dim str = String.Format("update PHONG set TenPhong='{0}', MaLoaiPhong={1}, TinhTrangPhong='{2}', GhiChu='{3}' where MaPhong={4}",
                                    phong.TenPhong, phong.MaLoaiPhong, phong.TinhTrangPhong, phong.GhiChu, phong.MaPhong)
            Dim dInserKH As New SQLProvider()
            Dim iKQ As Integer = -1
            iKQ = dInserKH.ExecNoneQuery(str)

            If (iKQ < 1) Then
                MessageBox.Show("Khong the cap nhat phong.")
            Else
                LoadPhong()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnXoaPhong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaPhong.Click
        Try
            Dim phong As New PhongDTO()
            phong.MaPhong = txtMaPhong.Text

            Dim str = String.Format("delete from PHONG where MaPhong={0}", phong.MaPhong)
            Dim dInserKH As New SQLProvider()
            Dim iKQ As Integer = -1
            iKQ = dInserKH.ExecNoneQuery(str)

            If (iKQ < 1) Then
                MessageBox.Show("Khong the xoa phong.")
            Else
                LoadPhong()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub grdPhongMoi_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdPhongMoi.SelectionChanged
        Try
            If (grdPhongMoi.Rows.Count > 0 And grdPhongMoi.SelectedRows.Count > 0) Then
                txtMaPhong.Text = grdPhongMoi.SelectedRows(0).Cells(0).Value
                txtTenPhong.Text = grdPhongMoi.SelectedRows(0).Cells(1).Value

                For i As Integer = 0 To arrLP.Length
                    If (arrLP(i) = Nothing) Then
                        Exit For
                    ElseIf (grdPhongMoi.SelectedRows(0).Cells(2).Value = arrLP(i).Split(",")(0)) Then
                        iMaLoaiPhong = arrLP(i).Split(",")(0).Trim()
                        cbbLoaiPhong.SelectedItem = arrLP(i).Split(",")(1).Trim()
                        txtDonGia.Text = arrLP(i).Split(",")(2).Trim()
                        Exit For
                    End If
                Next
                If (grdPhongMoi.SelectedRows(0).Cells(3).Value = "Co khach") Then
                    cbTinhTrangP.Checked = True
                Else
                    cbTinhTrangP.Checked = False
                End If
                txtGhiChu.Text = grdPhongMoi.SelectedRows(0).Cells(4).Value
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class